package com.cn.demo4.service.impl;

import com.cn.demo4.common.Tool;
import com.cn.demo4.entity.NewsEntity;
import com.cn.demo4.mapper.NewsMapper;
import com.cn.demo4.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

@Service
public class NewsServiceImpl implements INewsService {
    @Autowired
    private NewsMapper newsMapper;
    @Override
    public List<NewsEntity> findAll(NewsEntity newsEntity) {
        return newsMapper.findAll(newsEntity);
    }

    @Override
    public int getCount(NewsEntity newsEntity) {
        return newsMapper.getCount(newsEntity);
    }

    @Override
    public NewsEntity findById(Integer id) {
        return newsMapper.findById(id);
    }

    @Override
    public String add(NewsEntity newsEntity) {
        int result = newsMapper.add(newsEntity);
        if(result == 1) {
            Jedis jedis = new Jedis();
            jedis.flushDB();
        }
        //返回的是数据库影响条数故是int类型,当又数据更新是给一个标识符：success，反之亦然
        return result == 1 ? "success" : "error";
    }

    @Override
    public String update(NewsEntity newsEntity) {
        int result = newsMapper.update(newsEntity);
        if(result == 1) {
            Jedis jedis = new Jedis();
            jedis.flushDB();
        }
        return result == 1 ? "success" : "error";
    }

    @Override
    public int del(Integer id) {
        int result = newsMapper.del(id);
        if(result == 1){
            Jedis jedis = new Jedis( );
            jedis.flushDB();
        }
        return result;
    }

    @Override
    public List<NewsEntity> getNewsModelListForHome() {
        List<NewsEntity> list = new ArrayList<>();
        Jedis jedis = new Jedis();
        Boolean exists = jedis.exists("newsId");
        if (exists) {
            List<String> newsIdList = jedis.lrange("newsId", 0, -1);
            for (int i = 0; i < newsIdList.size(); i++) {
                NewsEntity newsEntity = new NewsEntity();
                newsEntity.setId(Tool.stringToInt(jedis.lindex("newsId", i)));
                newsEntity.setImg(jedis.lindex("newsImg", i));
                newsEntity.setTitle(jedis.lindex("newsTitle", i));
                newsEntity.setTypeName(jedis.lindex("newsTypeName", i));
                newsEntity.setPublisher(jedis.lindex("newsPublisher", i));
                newsEntity.setCreateTime(jedis.lindex("newsCreateTime", i));
                newsEntity.setUpdateTime(jedis.lindex("newsUpdateTime", i));
                newsEntity.setStatus(Tool.stringToInt(jedis.lindex("newsStatus", i)));
                newsEntity.setContent(jedis.lindex("newsContent", i));
                newsEntity.setImg(jedis.lindex("newsImg", i));
                newsEntity.setHref(jedis.lindex("newsHref", i));
                newsEntity.setClickCount(Tool.stringToInt(jedis.lindex("newsClickCount",i)));
                list.add(newsEntity);
            }
        } else {
            //查询到数据保存到redis中
            list = newsMapper.getNewsModelListForHome();
            for(int i = 0 ; i<list.size();i++){
                jedis.rpush("newsId",Integer.toString(list.get(i).getId()));
                jedis.rpush("newsStatus",Integer.toString(list.get(i).getStatus()));
                jedis.rpush("newsTitle",list.get(i).getTitle());
                jedis.rpush("newsTypeName",list.get(i).getTypeName());
                jedis.rpush("newsPublisher",list.get(i).getPublisher());
                jedis.rpush("newsCreateTime",list.get(i).getCreateTime());
                jedis.rpush("newsUpdateTime",list.get(i).getUpdateTime());
                jedis.rpush("newsContent",list.get(i).getContent());
                jedis.rpush("newsImg",list.get(i).getImg());
                jedis.rpush("newsHref",list.get(i).getHref());
                jedis.rpush("newsClickCount",Integer.toString(list.get(i).getClickCount()));
            }
        }
        return list;
    }

    @Override
    public List<NewsEntity> findDetailsById(Integer id) {
        return newsMapper.findDetailsById(id);
    }
}
